package com.yc.yyds.dao;

import com.yc.yyds.util.DBHelper;

import java.util.List;
import java.util.Map;

public class IndentDao {
    //查询订单信息
    public List<Map<String, Object>> query(String userid){
      String sql="select userid,indentid,ispay,indenttime,seat,money,Moviename,starttime,endtime,cinemaname,cinemadesc from ("
              +"(select indent.userid,ispay, indentid,indenttime,ppjhid,seat,indent.money from indent inner join user on indent.Userid=user.userid) i "
              +"INNER JOIN" +
              "(select ppjhid,Moviename,starttime,endtime  from arrange a INNER JOIN movie m on a.movieid = m.movieid)am " +
              "on i.ppjhid=am.ppjhid "
              +"INNER JOIN" +
              "(select c.cinemaname,cinemadesc, ppjhid  from arrange a2 INNER JOIN cinema c on a2.cinemaid=c.cinemaid) ac " +
              "ON ac.ppjhid=am.ppjhid) where userid=? order by indenttime desc ";
      return DBHelper.selectListMap(sql,userid);
    }
    //保存订单

    //取消订单
    public void cancel (String indentid,String userid,String ppjhid){

        String indentMoney = select(indentid);
        String userMoney = UserMoney(userid);
        tuiqian((Float.parseFloat(indentMoney)+Float.parseFloat(userMoney)),userid);
        add(seatNum(indentid),ppjhid);
        String sql = "delete from indent where indentid = ?";
        DBHelper.update(sql,indentid);
    }
    //查询订单金额
    public String select(String indentid){
        String sql = "select money from indent where indentid = ?";
        return DBHelper.selectOneMap(sql, indentid).get("money").toString();
    }
    //退钱
    public void tuiqian(float money,String userid){
        String sql ="update user set money=? where userid = ?";
        DBHelper.update(sql,money,userid);
    }
    //查用户金额
    public String UserMoney(String userid){
        String sql1 = "select money from user where userid=?";
       return DBHelper.selectOneMap(sql1, userid).get("money").toString();
    }
    //计算座位数量
    public int seatNum(String indentid){
        String sql ="select remainseats,Seat from arrange a INNER JOIN indent i on a.ppjhid=i.ppjhid where indentid = ?";
        Map<String, Object> map = DBHelper.selectOneMap(sql, indentid);
        String[] seats = map.get("seat").toString().trim().split("\\s");
        int remainseats= (int)map.get("remainseats");
        return remainseats+seats.length;
    }
    //加座位
    public void add (int seat,String ppjhid){
        String sql ="update arrange set remainseats = ? where ppjhid= ? ";
        DBHelper.update(sql,seat,ppjhid);
    }
}
